<?php
class LangUtil {
  private static $intl          = 'en';
  private static $messageCol    = null;

  private function  __construct() {

  }

  public static function setIntl($v) {
    self::$intl = strtolower($v);
  }

  public static function getIntl() {
    return self::$intl;
  }

  private static function _getInstance() {
    if (is_null(self::$messageCol)) {
      $fileName = sfConfig::get('sf_app_config_dir') . '/' . 'lang.' . self::$intl . '.yml';
      if (file_exists($fileName) && is_file($fileName)) {
        $data = sfYaml::load($fileName);
        if ($data) {
          self::$messageCol = $data;
        } else {
          return false;
        }
      } else {
        return false;
      }
    }
  }

  public static function getText($key, $variables = array()) {
    self::_getInstance();

    if (!is_null(self::$messageCol) && isset(self::$messageCol[$key])) {
      return self::replacePlaceholders(self::$messageCol[$key], $variables);
    } else {
      return '';
    }
  }

  private static function replacePlaceholders($text, $placeholders) {
    foreach ($placeholders as $index => $val) {
      $text = str_replace('{' . $index . '}', $val, $text);
    }
    return $text;
  }
}